// 简单的状态管理模式实现

// 定义应用状态
const state = {
  // 用户信息
  user: {
    isLoggedIn: false,
    username: '',
    avatar: ''
  },
  
  // 当前播放的歌曲信息
  currentSong: null,
  isPlaying: false,
  currentTime: 0,
  duration: 0,
  volume: 0.7,
  
  // 播放器状态
  playlist: [],
  currentPlaylist: null,
  currentPlaylistName: '',
  
  // 应用配置
  config: {
    autoPlayNext: true,
    rememberVolume: true
  }
};

// 定义mutations，用于修改状态
const mutations = {
  // 设置用户登录状态
  SET_USER_LOGIN(state, userData) {
    state.user.isLoggedIn = true;
    state.user.username = userData.username || '用户';
    state.user.avatar = userData.avatar || 'https://picsum.photos/id/1005/200/200';
    
    // 将用户信息保存到本地存储
    localStorage.setItem('user', JSON.stringify({
      ...userData,
      loggedIn: true,
      timestamp: new Date().getTime()
    }));
  },
  
  // 设置用户登出状态
  SET_USER_LOGOUT(state) {
    state.user.isLoggedIn = false;
    state.user.username = '';
    state.user.avatar = '';
    
    // 清除本地存储中的用户信息
    localStorage.removeItem('user');
  },
  
  // 设置当前播放的歌曲
  SET_CURRENT_SONG(state, song) {
    state.currentSong = song;
  },
  
  // 设置播放状态
  SET_PLAYING_STATE(state, isPlaying) {
    state.isPlaying = isPlaying;
  },
  
  // 设置当前播放时间
  SET_CURRENT_TIME(state, time) {
    state.currentTime = time;
  },
  
  // 设置歌曲总时长
  SET_DURATION(state, duration) {
    state.duration = duration;
  },
  
  // 设置音量
  SET_VOLUME(state, volume) {
    state.volume = volume;
    
    // 如果设置了记住音量，则保存到本地存储
    if (state.config.rememberVolume) {
      localStorage.setItem('player_volume', volume.toString());
    }
  },
  
  // 设置播放列表
  SET_PLAYLIST(state, songs) {
    state.playlist = songs;
  },
  
  // 设置当前歌单
  SET_CURRENT_PLAYLIST(state, playlist) {
    state.currentPlaylist = playlist.id;
    state.currentPlaylistName = playlist.name;
  },
  
  // 切换自动播放下一首设置
  TOGGLE_AUTO_PLAY_NEXT(state) {
    state.config.autoPlayNext = !state.config.autoPlayNext;
    localStorage.setItem('auto_play_next', state.config.autoPlayNext.toString());
  },
  
  // 初始化应用状态
  INIT_STATE(state) {
    // 从本地存储加载用户信息
    const user = JSON.parse(localStorage.getItem('user'));
    if (user && user.loggedIn) {
      state.user.isLoggedIn = true;
      state.user.username = user.username || '用户';
      state.user.avatar = user.avatar || 'https://picsum.photos/id/1005/200/200';
    }
    
    // 从本地存储加载音量设置
    const savedVolume = localStorage.getItem('player_volume');
    if (savedVolume) {
      state.volume = parseFloat(savedVolume);
    }
    
    // 从本地存储加载自动播放设置
    const autoPlayNext = localStorage.getItem('auto_play_next');
    if (autoPlayNext !== null) {
      state.config.autoPlayNext = autoPlayNext === 'true';
    }
  }
};

// 定义actions，用于处理异步操作
const actions = {
  // 用户登录
  login({ commit }, userData) {
    // 这里可以添加实际的登录API调用
    // 模拟异步操作
    return new Promise((resolve) => {
      setTimeout(() => {
        commit('SET_USER_LOGIN', userData);
        resolve();
      }, 500);
    });
  },
  
  // 用户登出
  logout({ commit }) {
    commit('SET_USER_LOGOUT');
  },
  
  // 播放歌曲
  playSong({ commit }, song) {
    commit('SET_CURRENT_SONG', song);
    commit('SET_PLAYING_STATE', true);
    commit('SET_CURRENT_TIME', 0);
  },
  
  // 初始化应用
  initApp(store) {
    // 在我们的自定义store实现中，直接调用mutations中的INIT_STATE方法
    if (store && store.mutations && store.mutations.INIT_STATE) {
      store.mutations.INIT_STATE(store.state);
    }
  }
};

// 定义getters，用于获取状态的派生值
const getters = {
  // 获取用户信息
  getUser: state => state.user,
  
  // 获取播放器状态
  getPlayerState: state => ({
    currentSong: state.currentSong,
    isPlaying: state.isPlaying,
    currentTime: state.currentTime,
    duration: state.duration,
    volume: state.volume
  }),
  
  // 获取当前播放列表
  getPlaylist: state => state.playlist,
  
  // 获取当前歌单信息
  getCurrentPlaylistInfo: state => ({
    id: state.currentPlaylist,
    name: state.currentPlaylistName
  })
};

// 创建store实例
const store = {
  state,
  mutations,
  actions,
  getters
};

// 导出store实例
export default store;